package yagwl.client.model.demo;

import java.util.List;

import yagwl.client.model.ItemDTO;
import yagwl.client.model.ItemListCache;
import yagwl.client.model.fnd.RpcStatus;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class ProductListCache extends ItemListCache {

  private CatalogRpcServiceAsync productService = GWT.create(CatalogRpcService.class);

  @Override
  public void fetchNext() {
    if (isLoadInProgress())
      return;
    loadStart();
    productService.getProducts(new AsyncCallback<List<ItemDTO>>() {
      @Override
      public void onFailure(Throwable caught) {
        onLoadFailure(caught);
      }

      @Override
      public void onSuccess(List<ItemDTO> result) {
        onLoadSuccess(result);
      }
    });
  }

  @Override
  public void deleteObject(final int objIdx) {
    productService.deleteProduct(getList().get(objIdx).getId(),
        new AsyncCallback<RpcStatus>() {

          @Override
          public void onFailure(Throwable caught) {
            Window.alert(caught.toString());
          }

          @Override
          public void onSuccess(RpcStatus result) {
            getList().remove(objIdx);
            throwDataReadyEvent();
          }
        });
  }

  @Override
  protected void handleSortOrderChange() {
  }

  @Override
  public void updateObject(Object object) {
  }

}
